<template>
	<!-- tab首页 -->
	<view>
		<!-- #ifndef MP-WEIXIN -->
		<car v-show="showHome==false" ref="car"></car>
		<carHome v-show="showHome==true"  ref="carHome"></carHome>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<car ref="car"></car>
		<!-- #endif -->
		<!-- <button @click="showHome=true" class="debug">D</button> -->

	</view>
</template>
<!--  -->
<script>
	const app = getApp();
	const util = require('@utils/util.js');
	const network = require('@manager/network-manager.js');
	import car from '@/pages/car/car.vue'


	import carHome from '@/pages/car/carHome/carHome.vue'
	// #ifdef APP-PLUS
	const YiLianModule = uni.requireNativePlugin('YiLianModule'); //调用
	// #endif
	export default {
		data() {
			return {
				showHome: false,
				myCarDataList: [],
				debug: false,
				isLogin: false

			}
		},
		components: {
			carHome,
			car
		},
		onLoad() {
			
			// this.carShow()
			let platform = uni.getSystemInfoSync().platform;
			if (platform == 'android') {
				//授权隐私后启动sdks
				console.log("android YiLianModule initSDKs START");
				YiLianModule.initSDKs({}, result => {
					console.log("YiLianModule initSDKs：" + JSON.stringify(result));
				});
			}
			
		},
		onShow() {
			 
			this.carShow()
			this.debug = app.globalData.config.dev != 'prd'
		},
		methods: {
			carShow: function() {
				var that = this;
				if (util.isLogin(app) == false) {
					that.isLogin = false
				} else {
					that.isLogin = true
				}
				//#ifndef MP-WEIXIN 
				if (that.myCarDataList.length > 0 && that.isLogin==true) {
					that.showHome = true
				} else {
					that.showHome = false
				}

				if (that.isLogin == false) {
					that.$nextTick(function() {
						if (that.$refs.car) {
							that.$refs.car.car_onShow()
						}
					})
				} else {
					that.getMyCarList()
				}
				//#endif

				//#ifndef MP-WEIXIN
				//v-if后要在下一个页面循环获取ref
				// that.$nextTick(function() {
				// 	if (util.isLogin(app)==false) {
				// 		if(that.$refs.car){
				// 			that.$refs.car.car_onShow()
				// 		}
				// 	} else {
				// 		that.getMyCarList()
				// 	}
				// })
				//#endif
				//#ifdef MP-WEIXIN
				if (that.$refs.car) {
					that.$refs.car.car_onShow()
				}
				//#endif


			},



			//查询我的爱车列表
			getMyCarList: function() {
				var that = this;
				var uri = app.globalData.config.interfaces.URL_CMOBILE_VEHICLEINFO_GETMYCARLIST;
				var params = {
					userId: app.getUserId()
				};
				console.log(params, 'params')
				network.get(uri, params, 'form').then(res => {
					if (res.code == 200) {
						var list = res.data
						that.myCarDataList = []
						
						
						
						for (var j = 0; j < list.length; j++) {
							if (list[j].deviceId != "" && list[j].deviceId != null && list[j].deviceId !=
								undefined) {
								that.myCarDataList.push(list[j])
							}
						}
						this.$nextTick(function() {
							if (that.myCarDataList.length > 0 && that.isLogin==true) {
								that.showHome = true
								that.$refs.carHome.carHome_onShow(that.myCarDataList)
							} else {
								that.showHome = false
								that.$refs.car.car_onShow()
							}
						})
						

						
				 
					}
				})
			},

		}
	}
</script>

<style scoped>
	.debug {
		position: fixed;
		top: 200upx;
		right: 20upx;
		width: 80upx;
		line-height: 80upx;
		border-radius: 40upx;
		height: 80upx;
		background-color: greenyellow;
		color: black;

	}
</style>
